package com.enmaster.dao;

import com.enmaster.domain.Vocabulary;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface VocabularyDao {


    /**
     *  根据所选词库创建个人词库
     *  库名为用户ID
     * @param userID 用户ID
     * @param bookName  书名
     */
    @Update("create table ${userID} select * from ${bookName};"+
            "ALTER TABLE ${userID} ADD CONSTRAINT ${userID}_UN UNIQUE KEY (word);")
    void initial(@Param("userID") String userID,String bookName);

    /**
     * 根据传入的参数进行模糊查询
     * %Word% 含有
     * %Word  结尾
     *  Word% 开头
     * @param userID 用户ID--表名
     * @param word  单词
     * @return 单词列表
     */
    @Select("select * from ${userID} where word like '${word}%'")
    List<Vocabulary> findWord(String userID, String word);

    /**
     * 根据天数从词库中随机选取 x 个单词
     * @param userID 表名
     * @param x 单词数
     * @return 单词列表
     */
    @Select("SELECT * FROM ${userID} where grade = ${day} ORDER BY RAND() limit ${x}  ")
    List<Vocabulary> randWord(String userID,Integer x,Integer day);

    /**
     * 根据天数顺序选取 x 个单词
     * @param userID 表名
     * @param x 个数
     * @param day 天数
     * @return 单词列表
     */
    @Select("SELECT * FROM ${userID} where grade = ${day} limit ${x} ")
    List<Vocabulary> selectWord(String userID,Integer x,Integer day);

    /**
     * 添加单词到个人词库
     * 不可重复添加 —— 重复异常：java.sql.SQLIntegrityConstraintViolationException
     * @param userID 表名
     * @param vocabulary 单词
     */
    @Insert("insert into ${userID} " +
            "set word=#{Vocabulary.word}," +
            "phonetic =#{Vocabulary.phonetic}," +
            "definition =#{Vocabulary.definition}," +
            "`translation` =#{Vocabulary.translation}," +
            "exchange =#{Vocabulary.exchange}")
    void insertVocabulary(String userID,@Param("Vocabulary") Vocabulary vocabulary);
}
